<?php

namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;

class CorporateAccountRepository
{
    /**
     * @Desc:对公账户列表
     * @param $param
     * @return mixed
     * @author: shiquan
     * @Time: 2022/9/28 23:12
     */
    public function list($param)
    {
        $userBank = app('model_user_bank');
        $userData = app('model_user_data');

        $list = app('model_user_bank')->from($userBank->getTable() . " as t1")
            ->select(['t1.*', 't2.nickname', 't2.user_code'])
            ->leftJoin(DB::raw($userData->getTable(). ' as t2'),'t1.user_id','=','t2.user_id')
            ->where(function ($query) use ($param) {
                if (isset($param['user_code'])) {
                    $query->where('t2.user_code', $param['user_code']);
                }
                if (isset($param['bank_num']) && $param['bank_num']) {
                    $query->where('bank_num', 'like','%'.$param['bank_num'].'%');
                }
                if (isset($param['mobile']) && $param['mobile']) {
                    $query->where('t1.mobile', 'like','%'.$param['mobile'].'%');
                }
            })->where('t1.bank_type', 4)->where('t1.is_deleted',0)
            ->orderBy('id', 'desc')
            ->paginate($param['page_size'] ?? 1, ['*'], '', $param['page'] ?? 10);

        foreach ($list as &$item) {
            //对联系方式和银行卡号脱敏处理
            $item->mobile = strReplaces($item->mobile, 3,4,'*');
            $item->bank_num = strReplaces($item->bank_num, 4,8,'*');
            $item->add_time = date('Y-m-d H:i:s', $item->add_time);
        }
        return $list;
    }

    /**
     * @Desc:新增
     * @param $param
     * @return bool
     * @author: shiquan
     * @Time: 2022/9/28 23:32
     */
    public function create($param)
    {
        $user = app('model_user_data')->where('user_code', $param['user_code'])->first();
        if (empty($user)){
            throw new BasicException(30002);
        }
        $param['user_id'] = $user['user_id'];
        $param['add_time'] = time();
        $param['bank_type'] = 4;        //对公账户
        $param['bank_name'] = $param['bank_zhi'];
        $res = app('model_user_bank')->create($param);
        if ($res){
            return $res;
        } else {
            throw new BasicException(0, '添加失败');
        }
    }

    /**
     * @Desc:详情
     * @param $param
     * @return mixed
     * @author: shiquan
     * @Time: 2022/9/28 23:34
     */
    public function info($param)
    {
        $userBank = app('model_user_bank');
        $userData = app('model_user_data');
        $info = app('model_user_bank')->from($userBank->getTable() . " as t1")->select(['t1.*', 't2.user_code', 't2.nickname'])
            ->leftJoin(DB::raw($userData->getTable(). ' as t2'),'t1.user_id','=','t2.user_id')
            ->where('t1.id', $param['id'])
            ->first();
        return $info;
    }

    /**
     * @Desc:更新
     * @param $param
     * @return bool
     * @author: shiquan
     * @Time: 2022/9/28 23:34
     */
    public function update($param)
    {
        $id = $param['id'];
        $user = app('model_user_data')->where('user_code',$param['user_code'])->first();
        if (empty($user)){
            throw new BasicException(30002);
        }
        $param['user_id'] = $user['user_id'];
        unset($param['id']);
        unset($param['user_code']);
        unset($param['nickname']);
        $param['update_time'] = time();
        $param['bank_name'] = $param['bank_zhi'];   //开户行和开户支行存一样的
        $res = app('model_user_bank')->where('id', $id)->update($param);
        if ($res){
            return $res;
        } else {
            throw new BasicException(0, '编辑失败');
        }
    }

    /**
     * @Desc:删除
     * @param $param
     * @return bool
     * @author: shiquan
     * @Time: 2022/9/28 23:35
     */
    public function delete($param)
    {
        $res = app('model_user_bank')->whereIn('id', $param['ids'])->update(['is_deleted' => 1]);
        if ($res){
            return $res;
        } else {
            throw new BasicException(0, '删除失败');
        }
    }

    /**
     * @Desc:根据用户编号获取用户昵称
     * @param $param
     * @return mixed
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/10/8 16:23
     */
    public function userInfo($param)
    {
        $user = app('model_user_data')->select('nickname')->where('user_code', $param['user_code'])->first();
        if (empty($user)){
            throw new BasicException(0, '请输入正确的用户编号');
        }
        return $user;
    }
}
